package com.ybl.store.mp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ybl.store.backend.dao.entity.customer.CustomerProductDO;
import com.ybl.store.backend.dao.mapper.customer.CustomerProductMapper;
import com.ybl.store.backend.dao.mapper.customer.CustomerVoucherMapper;
import com.ybl.store.backend.dto.resp.user.customer.CustomerProductNumberDTO;
import com.ybl.store.backend.dto.resp.user.customer.CustomerVoucherNumberDTO;
import com.ybl.store.common.constant.BanFlag;
import com.ybl.store.common.entity.ListRespDTO;
import com.ybl.store.common.properties.WxProperties;
import com.ybl.store.common.util.WeChatUtil;
import com.ybl.store.common.constant.DelFlag;
import com.ybl.store.common.convention.errorcode.BaseErrorCode;
import com.ybl.store.common.convention.exception.ClientException;
import com.ybl.store.backend.dao.entity.user.CustomerDO;
import com.ybl.store.mp.dao.mapper.MpCustomerMapper;
import com.ybl.store.mp.service.MpService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class MpServiceImpl implements MpService {

    private final MpCustomerMapper customerMapper;
    private final CustomerProductMapper customerProductMapper;
    private final CustomerVoucherMapper customerVoucherMapper;
//    private final MpCustomerGoodMapper customerGoodMapper;
    private final WxProperties wxProperties;
    @Override
    public String login(String code) {
        String appid = wxProperties.getAppId();
        String appSecret = wxProperties.getAppSecret();
        String tokenUrl = wxProperties.getTokenUrl();
        String openid = WeChatUtil.getOpenid(appid,appSecret,tokenUrl,code);
//        String openid = code;
        if (Objects.isNull(openid)) {
            throw new ClientException(BaseErrorCode.MP_OPENID_REGISTER_ERROR);
        }
        LambdaQueryWrapper<CustomerDO> lambdaQueryWrapper = Wrappers.lambdaQuery(CustomerDO.class)
                .eq(CustomerDO::getOpenid, openid)
                .eq(CustomerDO::getDelFlag, DelFlag.present)
                .eq(CustomerDO::getBanFlag, BanFlag.use);
        CustomerDO customerDO = customerMapper.selectOne(lambdaQueryWrapper);

        if (customerDO == null) {
            throw new ClientException(BaseErrorCode.CUSTOMER_NULL_ERROR);
        }

        return customerDO.getId().toString();
    }

    @Override
    public String register(String code, String phone) {
        String appid = wxProperties.getAppId();
        String appSecret = wxProperties.getAppSecret();
        String tokenUrl = wxProperties.getTokenUrl();
        LambdaQueryWrapper<CustomerDO> lambdaQueryWrapper = Wrappers.lambdaQuery(CustomerDO.class)
                .eq(CustomerDO::getPhone, phone)
                .eq(CustomerDO::getDelFlag, DelFlag.present)
                .eq(CustomerDO::getBanFlag, BanFlag.use);
        CustomerDO customerDO = customerMapper.selectOne(lambdaQueryWrapper);

        if (customerDO == null) {
            throw new ClientException(BaseErrorCode.CUSTOMER_NULL_ERROR);
        }
        if (customerDO.getOpenid() != null) {
            throw new ClientException(BaseErrorCode.MP_OPENID_EXIST_ERROR);
        }

        String openid = WeChatUtil.getOpenid(appid,appSecret,tokenUrl,code);
        if(openid == null){
            throw new ClientException(BaseErrorCode.MP_OPENID_NULL_ERROR);
        }
        customerDO.setOpenid(openid);

        int result = customerMapper.updateById(customerDO);
        if (result < 1) {
            throw new ClientException(BaseErrorCode.CUSTOMER_UPDATE_ERROR);
        }

        return customerDO.getId().toString();
    }
    @Override
    public ListRespDTO<CustomerProductNumberDTO> listProduct(Long id){
        QueryWrapper<CustomerProductNumberDTO> QueryWrapper = Wrappers.query(CustomerProductNumberDTO.class)
                .eq(Objects.nonNull(id),"t_deal_product.customer_id", id)
                .eq("t_deal_product.del_flag", 0)
                .eq("t_deal_product.deal_status", 0);
        List<CustomerProductNumberDTO> customerProductDTOS = customerProductMapper.selectCustomerProductByCustomerId(QueryWrapper);
        return ListRespDTO
                .<CustomerProductNumberDTO>builder()
                .total((long) customerProductDTOS.size())
                .items(customerProductDTOS)
                .build();
    }
    @Override
    public ListRespDTO<CustomerVoucherNumberDTO> listVoucher(Long id){
        QueryWrapper<CustomerVoucherNumberDTO> QueryWrapper = Wrappers.query(CustomerVoucherNumberDTO.class)
                .eq(Objects.nonNull(id),"t_deal_voucher.customer_id", id)
                .eq("t_deal_voucher.del_flag", 0)
                .eq("t_deal_voucher.type", 0);
        List<CustomerVoucherNumberDTO> customerVoucherNumberDTOS = customerVoucherMapper.selectCustomerVoucherByCustomerId(QueryWrapper);
        return ListRespDTO
                .<CustomerVoucherNumberDTO>builder()
                .total((long) customerVoucherNumberDTOS.size())
                .items(customerVoucherNumberDTOS)
                .build();
    }
//    @Override
//    public ListRespDTO<CustomerGoodDO> listGood(Long id) {
//        LambdaQueryWrapper<CustomerGoodDO> lambdaQueryWrapper = Wrappers.lambdaQuery(CustomerGoodDO.class)
//                .eq(CustomerGoodDO::getCustomerId, id)
//                .eq(CustomerGoodDO::getDelFlag, DelFlag.present);
//        List<CustomerGoodDO> customerGoodList = customerGoodMapper.selectList(lambdaQueryWrapper);
//
//        return ListRespDTO
//                .<CustomerGoodDO>builder()
//                .total((long) customerGoodList.size())
//                .items(customerGoodList)
//                .build();
//    }
}
